package com.ruoyi.system.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.system.api.domain.SysDatascopeData;
import com.ruoyi.system.service.ISysDatascopeDataService;

/**
 * 数据权限数据
 * @author breeze
 * @date 2024-01-16
 */
@RestController
@RequestMapping("/datascope/data")
public class SysDatascopeDataController extends BaseController{
    
    @Autowired
    private ISysDatascopeDataService sysDatascopeDataService;

    @GetMapping("/list")
    public TableDataInfo<?> list(SysDatascopeData sysDatascopeData){
        startPage();
        List<SysDatascopeData> list = sysDatascopeDataService.selectSysDatascopeDataList(sysDatascopeData);
        return getDataTable(list);
    }

    @GetMapping(value = "/{datascopeCode}")
    public AjaxResult getInfo(@PathVariable("datascopeCode") String datascopeCode){
        return AjaxResult.success(sysDatascopeDataService.selectSysDatascopeDataByDatascopeCode(datascopeCode));
    }

    @Log(title = "新增数据权限子数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addData(@RequestBody SysDatascopeData sysDatascopeData){
        return toAjax(sysDatascopeDataService.saveData(sysDatascopeData));
    }

    @Log(title = "保存数据权限子数据", businessType = BusinessType.INSERT)
    @PostMapping("/removeAndSave")
    public AjaxResult removeAndSaveData(@RequestBody SysDatascopeData sysDatascopeData){
        return toAjax(sysDatascopeDataService.removeAndSaveData(sysDatascopeData));
    }
    
    @PutMapping
    public AjaxResult edit(@RequestBody SysDatascopeData sysDatascopeData){
        return toAjax(sysDatascopeDataService.updateById(sysDatascopeData));
    }

    /**
     * 传入@PathVariable的格式类似为/1,2,3,4,5时,会自动转为数组
     */
    @Log(title = "数据权限数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{datascopeCodes}")
    public AjaxResult remove(@PathVariable String[] datascopeCodes){
        return toAjax(sysDatascopeDataService.removeByIds(Arrays.asList(datascopeCodes)));
    }
    
    @Log(title = "删除数据权限子数据", businessType = BusinessType.DELETE)
    @DeleteMapping
    public AjaxResult removeData(@RequestBody SysDatascopeData sysDatascopeData){
        return toAjax(sysDatascopeDataService.removeDataByMap(sysDatascopeData));
    }
    
    /**
     * 导出数据权限数据列表
     */
    @Log(title = "数据权限数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDatascopeData sysDatascopeData){
        List<SysDatascopeData> list = sysDatascopeDataService.selectSysDatascopeDataList(sysDatascopeData);
        ExcelUtil<SysDatascopeData> util = new ExcelUtil<SysDatascopeData>(SysDatascopeData.class);
        util.exportExcel(response, list, "数据权限数据数据");
    }
    
}
